<?php
namespace app\souther\controller;

use app\common\model\Plug as PlugModel;
use \app\common\model\Menu;
use think\Db;

class Plug extends Base
{
	
	function __construct()
	{
		parent::_initialize();
	}


	/**
     * [index  列表]
     * @author [南風]
     * @return mixed|string
     * @throws \Exception
     */
    public function index()
    {
        $model = new PlugModel();

        if(request()->isAjax())
        {
            $data = input('param.');
            $map = [];

            if(is_array($data)){
                if(!empty($data['key']))
                    $map['name'] = ['like',"%" . $data['key'] . "%"];
            }
            $this->getPageAndSize($data);

            try{
                $count =$model->getPlugCountByCondition($map);
                $allpage = intval(ceil($count / $this->size));//总页面
                $lists = $model->getPlugByCondition($map, $this->from, $this->size);//列表
            }catch (\Exception $e)
            {
                writelog($e->getMessage(),-1);
                exception($e->getMessage());
            }

            if(empty($lists))
                return '';
            
            writelog('查看插件列表');

            return $this->fetch('ajax_list',[
                'count' => $count,//总记录数
                'allpage' => $allpage,
                'lists' => $lists,
            ]);
        }

        return $this->fetch('',['app'=>'plug']);
    }

    /**
     * [edit  安装模块]
     * @author [南風]
     * @return array|mixed
     */
    public function install()
    {

        $model = new PlugModel();
        $menu = new Menu();

        $id = input('param.id');

        $result = $model->where('id',$id)->field('module_id,type')->find();
        
        if(!empty($result)){
            $menu_data = ['id'=>$result['module_id'],'status'=>1,'install'=>1];
        }
        
        $plud_data = ['id'=>$id,'status'=>1,'install'=>1];

        try {
            $model->editData($plud_data);
            if($result['type']==1){
                $menu->editData($menu_data);
            }
        } catch (Exception $e) {
            writelog($e->getMessage(),-1);
            exception($e->getMessage());
        }

        return $this->redirect('/plug');
    }

    /**
     * [edit  卸载模块]
     * @author [南風]
     * @return array|mixed
     */
    public function uninstall()
    {

        $model = new PlugModel();
        $menu = new Menu();

        $id = input('param.id');

        // 模块ID
        $result = $model->where('id',$id)->field('module_id,type')->find();
        if(!empty($result)){
            $menu_data = ['id'=>$result['module_id'],'status'=>0,'install'=>0];

            // 模块下面所有子模块
            $module = $menu->where(['pid'=>$result['module_id']])->field('route_url')->select();
        }
        // 插件ID
        $plud_data = ['id'=>$id,'status'=>0,'install'=>0];

        try {
            $model->editData($plud_data);
            
            if($result['type']==1){
                $menu->editData($menu_data);
            }
            // 循环所有子模块 清空数据
            foreach ($module as $key => $value) {
                Db::execute('TRUNCATE trc_'.$value['route_url']);
            }
        } catch (Exception $e) {
            writelog($e->getMessage(),-1);
            exception($e->getMessage());
        }

        return $this->redirect('/plug');
    }

    /**
     * [status 状态]
     * @author [南風]
     * @return array
     */
    public function status()
    {
        $model = new PlugModel();
        $menu = new Menu();

        $data=input('param.');
        $res = $model->changeStatus(['id' => $data['id']],$data['status']);


        if($res)
        {
            $result = $model->where('id',$data['id'])->field('module_id')->find();
            $menu->changeStatus(['id' => $result['module_id']],$data['status']);

            if($data['status'] == 0)
            {
                $statusurl = url('status',['id'=>$data['id'],'status'=>1]);
                return admin_json(2,'已禁止',['statusurl'=>$statusurl]);
            }
            else
            {
                $statusurl = url('status',['id'=>$data['id'],'status'=>0]);
                return admin_json(1,'已启用',['statusurl'=>$statusurl]);
            }
        }
        return admin_json(-1,'操作失败');
    }

    /**
     * [deploy 模块配置]
     * @author [南風]
     * @return array
     */
    public function deploy()
    {
        $model = new PlugModel();
        $menu = new Menu();

        $id =input('param.id');

        // 查询当前安装模块id
        $module = $model->where('id',$id)->field('module_id')->find();
        if(!empty($module)){
            $result = $menu->where(['pid'=>$module['module_id']])->field('route_url')->find();
            $table = '';
            if(!empty($result))
                $table = $result['route_url'];

            if($table)
                return $this->redirect('/'.$table);

            return admin_json(-1,'找不到模块');
        }else{
            return admin_json(-1,'无效的模块ID');
        }
    }
}